/**
 * 
 */
package net.homelinux.ajay.net.ondisk.view.client;


import net.homelinux.ajay.net.ondisk.model.client.Folder;
import net.homelinux.ajay.net.ondisk.model.client.Link;
import net.homelinux.ajay.net.ondisk.view.dialogs.client.AddFolderDialog;
import net.homelinux.ajay.net.ondisk.view.dialogs.client.AppDialogBox;
import net.homelinux.ajay.net.ondisk.view.menu.client.FolderMenuBuilder;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author ajay
 *
 */
public class FolderTreeItem extends AppTreeItem {

  private static AppTreeItem dragTarget = CollectionCommandsTreeItem.getDragTarget();
  
  private CommandsMenu contextMenu ;

  private Folder folder;

  private final static String imageUrl = "images/gnome-folder.png";

  public boolean addFolder(String newFolder) {
    if(checkExists(newFolder)){
      return false;
    }
    makeFolder(newFolder, this);
    return true;
  }
  
  private boolean checkExists(String newFolder) {
    return (getModel().getFolder(newFolder)!=null);
  }
  
  private Folder getModel() {
    return folder;
  }
  /**
   * 
   */
  public FolderTreeItem() {
  }

//  /**
//   * @param html
//   */
//  public FolderTreeItem(String name) {
//    this(name, getImage());
//    
//  }
  
//  public FolderTreeItem(String name, Image image) {
//    super(name, image);
//  }
  
  public FolderTreeItem(String name, Folder model) {
    this(name,model,getImage());
  }

  public FolderTreeItem(String name, Folder model, Image image) {
    super(name,image);
    this.folder = model;
  }

  private static Image getImage() {
    Image img = new Image(imageUrl);
    img.setSize("24", "24");
    return img;
  }

//  /**
//   * @param widget
//   */
//  public CollectionTreeItem(Widget widget) {
//    super(widget);
//  }
  
  public static FolderTreeItem makeFolder(String content,FolderTreeItem parent) {
    FolderTreeItem itm2;
    Folder childfolder = parent.getModel().makeFolder(content);
    itm2 = new FolderTreeItem(content,childfolder);
//  String itemwidth = "10em";
//  itm2.setWidth(itemwidth);
//  if(parent!=null){
  
  parent.addItem(itm2);
//    }
    return itm2;
  }

  public AppTreeItem makeLinkChild(String text) {
    AppTreeItem itm = LinkTreeItem.makeChild(text, this);
    return itm;
  }

  public void onDeselect() {
    super.onDeselect();
    //removeItem(getDragTarget());
  }

  public void onSelect() {
    //addItem(getDragTarget());
    
    super.onSelect();
    
    setState(true);
  }

  public void onModelChange(/* FolderModel model */) {
    // TODO implement onMetaChange
    
  }

  protected CommandsMenu getCommandsMenu() {
    if(contextMenu==null){
      contextMenu = FolderMenuBuilder.getInstance().makeContextMenu();
    }
    return contextMenu;
  }

  public void removeFolderFromParent() {
    folder.removeFolderFromParent();
    remove();
  }

  public Folder getFolder() {
    // TODO Auto-generated method stub
    return folder;
  }
  

}
